// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/core/query_ctx.h>
#include <pollux/expression/cast_hooks.h>

namespace kumo::pollux::functions::sparksql {
    // This class provides cast hooks following Spark semantics.
    class SparkCastHooks : public exec::CastHooks {
    public:
        explicit SparkCastHooks(const pollux::core::QueryConfig &config);

        // TODO: Spark hook allows more string patterns than Presto.
        Expected<Timestamp> castStringToTimestamp(
            const StringView &view) const override;

        /// When casting integral value as timestamp, the input is treated as the
        /// number of seconds since the epoch (1970-01-01 00:00:00 UTC).
        Expected<Timestamp> castIntToTimestamp(int64_t seconds) const override;

        /// When casting double as timestamp, the input is treated as
        /// the number of seconds since the epoch (1970-01-01 00:00:00 UTC).
        Expected<std::optional<Timestamp> > castDoubleToTimestamp(
            double value) const override;

        /// 1) Removes all leading and trailing UTF8 white-spaces before cast. 2) Uses
        /// non-standard cast mode to cast from string to date.
        Expected<int32_t> castStringToDate(
            const StringView &dateString) const override;

        // Allows casting 'NaN', 'Infinity', '-Infinity', 'Inf', '-Inf', and these
        // strings with different letter cases to real.
        Expected<float> castStringToReal(const StringView &data) const override;

        // Allows casting 'NaN', 'Infinity', '-Infinity', 'Inf', '-Inf', and these
        // strings with different letter cases to double.
        Expected<double> castStringToDouble(const StringView &data) const override;

        /// When casting from string to integral, floating-point, decimal, date, and
        /// timestamp types, Spark hook trims all leading and trailing UTF8
        /// whitespaces before cast.
        StringView removeWhiteSpaces(const StringView &view) const override;

        const TimestampToStringOptions &timestampToStringOptions() const override {
            return timestampToStringOptions_;
        }

        bool truncate() const override {
            return true;
        }

        exec::PolicyType getPolicy() const override;

    private:
        // Casts a number to a timestamp. The number is treated as the number of
        // seconds since the epoch (1970-01-01 00:00:00 UTC).
        // Supports integer and floating-point types.
        template<typename T>
        Expected<Timestamp> castNumberToTimestamp(T seconds) const;

        const core::QueryConfig &config_;

        /// 1) Does not follow 'isLegacyCast'. 2) The conversion precision is
        /// microsecond. 3) Does not append trailing zeros. 4) Adds a positive
        /// sign at first if the year exceeds 9999. 5) Respects the configured
        /// session timezone.
        TimestampToStringOptions timestampToStringOptions_ = {
            .precision = TimestampToStringOptions::Precision::kMicroseconds,
            .leadingPositiveSign = true,
            .skipTrailingZeros = true,
            .zeroPaddingYear = true,
            .dateTimeSeparator = ' '
        };
    };
} // namespace kumo::pollux::functions::sparksql
